package game.module.qzmj.processor;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.offline.bean.PlayerOfflineCache;
import game.module.offline.logic.PlayerOfflineManager;
import game.module.qzmj.bean.QzmjRoom;
import game.module.qzmj.logic.QzmjDataManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageQzmj.S2CQzmjJoinRoom;
import lua.LuaMessageQzmj.S2CQzmjBackRoomHall;
import lua.LuaMessageQzmj.C2SQzmjBackRoomHall;
import lua.LuaMessageQzmj.QzmjMemberInfo;

@MsgCodeAnn(msgcode = C2SQzmjBackRoomHall.id,accessLimit = 500)
public class QzmjBackRoomProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger =  LoggerFactory.getLogger(QzmjBackRoomProcessor.class);
	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		// TODO Auto-generated method stub
		int playerId = playingRole.getId();
		logger.info("qzmj back to hall room,playeyId={}", playerId);
		// 是否在房间中 
		boolean isInRoom = QzmjDataManager.getInstance().isPlayerInRoom(playerId);
		if (!isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(106);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		//房间是否存在
		QzmjRoom ddzRoom = QzmjDataManager.getInstance().getMjRoomByPlayerId(playerId);
		if (ddzRoom == null) {
			S2ErrorCode respMsg = new S2ErrorCode(104);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		//ret 
		String room_id = ddzRoom.getUuid();
		S2CQzmjBackRoomHall respMsg = new S2CQzmjBackRoomHall();
		respMsg.hall_info = new S2CQzmjJoinRoom();
		respMsg.hall_info.room_id = room_id;
		respMsg.hall_info.round_count = ddzRoom.getRoundCount();
		respMsg.hall_info.sanjindao = ddzRoom.getSanjindao();
		respMsg.hall_info.eat_card = ddzRoom.getEat_card();
		respMsg.hall_info.hua_card = ddzRoom.getHua_card();
		respMsg.hall_info.prevent_cheat = ddzRoom.getPrevent_cheat();
		respMsg.hall_info.gold_rate = ddzRoom.getGold_coin();
		respMsg.hall_info.ready = ddzRoom.getReady();
		respMsg.hall_info.youjin = ddzRoom.getYoujin();
		respMsg.hall_info.cost_type = ddzRoom.getCost_type();
		respMsg.hall_info.members = new QzmjMemberInfo[ddzRoom.getPlayerList().size()];
		List<Integer> playerIdList = ddzRoom.getPlayerList();
		Map<Integer,String> ipmaps = ddzRoom.getIpmaps();
		int i = 0;
		for(Integer aPlayerId : playerIdList) {
			PlayerOfflineCache poc = PlayerOfflineManager.getInstance().getPlayerOfflineCache(aPlayerId);
			long jindou  = poc.getJindou();
			respMsg.hall_info.members[i++] = new QzmjMemberInfo(aPlayerId, poc.getHeadimg(), poc.getLevel(),
					poc.getSex().shortValue(),(int)jindou, ipmaps.get(aPlayerId),poc.getName());
		}
		playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
